/**
 * UsersController.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * May 4, 2015
 */
package com.ovt.doorbell.payment.api.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ovt.doorbell.payment.api.response.DoorbellPaymentAPIResult;
import com.ovt.doorbell.payment.common.model.JsonDocument;
import com.ovt.doorbell.payment.common.utils.CookieUtil;
import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.config.AppProperties;
import com.ovt.doorbell.payment.dao.constant.ResourceType;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.User;
import com.ovt.doorbell.payment.dao.vo.Permission;
import com.ovt.doorbell.payment.dao.vo.Resource;
import com.ovt.doorbell.payment.service.ResourceService;
import com.ovt.doorbell.payment.service.UserService;
import com.ovt.doorbell.payment.service.context.SessionContext;
import com.ovt.doorbell.payment.service.exception.ServiceException;

/**
 * UsersController provides restful APIs of user
 * 
 * @Author zhi.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */

@Controller
@RequestMapping("/resource")
public class ResourceController
{
    @Autowired
    private ResourceService resourceService;

    private static final JsonDocument SUCCESS = DoorbellPaymentAPIResult.SUCCESS;

    /**
     * Get resource list.
     * 
     * @param page
     * @param limit
     * @param sortBy
     * @param order
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/listAll")
    @ResponseBody
    public JsonDocument getResourceList(@RequestParam(required = false,
            defaultValue = "0") String page, @RequestParam(required = false,
            defaultValue = "20") String limit, @RequestParam(required = false,
            defaultValue = "id") String sortBy, @RequestParam(required = false,
            defaultValue = "asc") String order) throws ServiceException
    {
        PageInfo pageInfo =
                new PageInfo(DataConvertUtils.toInt(page),
                        DataConvertUtils.toInt(limit), sortBy, order);

        List<Resource> resourceList = resourceService.getAllResources(pageInfo);

        return new DoorbellPaymentAPIResult(resourceList);
    }

    /**
     * Create an user.
     * 
     * @param email
     * @param password
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/add")
    @ResponseBody
    public JsonDocument createResource(
            @RequestParam String code, @RequestParam String name,
            @RequestParam String type, @RequestParam String unit,
            @RequestParam float price, @RequestParam int size) 
                    throws ServiceException
    {
        Resource resource = new Resource();
        Timestamp stamp = new Timestamp((new Date()).getTime());
        resource.setCode(code);
        resource.setCreateTime(stamp);
        resource.setName(name);
        resource.setPrice(price);
        resource.setSize(size);
        resource.setType(ResourceType.getResourceType(type));
        resource.setUnit(unit);
        resource.setUpdateBy((int) SessionContext.getCurrentUser().getId());
        resource.setUpdateTime(stamp);
        this.resourceService.addResource(resource);
        
        return SUCCESS;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/change")
    @ResponseBody
    public JsonDocument updateResource(
            @RequestParam String code, @RequestParam String name,
            @RequestParam String type, @RequestParam String unit,
            @RequestParam float price, @RequestParam int size) 
                    throws ServiceException
    {
        Resource resource = new Resource();
        Timestamp stamp = new Timestamp((new Date()).getTime());
        resource.setCode(code);
        resource.setName(name);
        resource.setPrice(price);
        resource.setSize(size);
        resource.setType(ResourceType.getResourceType(type));
        resource.setUnit(unit);
        resource.setUpdateBy((int) SessionContext.getCurrentUser().getId());
        resource.setUpdateTime(stamp);
        this.resourceService.updateResource(resource);
        
        return SUCCESS;
    }
    /**
     * Create an admin.
     * 
     * @param email
     * @param password
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/{id}/delete")
    @ResponseBody
    public JsonDocument deleteResource(@PathVariable int id)
            throws ServiceException
    {
        resourceService.deleteResource(id);

        return SUCCESS;
    }

    /**
     * Create an admin.
     * 
     * @param email
     * @param password
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/price")
    @ResponseBody
    public JsonDocument getPrice(@RequestParam int id)
            throws ServiceException
    {
        float price = resourceService.getPrice(id);
        return new DoorbellPaymentAPIResult(price);
    }
    
    
}
